<html>
    <head>
        <meta charset="utf-8">
        <title>大话西游答题器 command line</title>
        <link rel="stylesheet" href="../assets/stylesheets/global.css">
        <link rel="stylesheet" href="../assets/stylesheets/words.css">
        <link rel="stylesheet" href="../assets/stylesheets/monokai.css">
        <link rel="stylesheet" href="../assets/stylesheets/table.css">
        <link rel="shortcut icon" href="../assets/images/favicon.ico" type="image/x-icon">
        <link rel="icon" href="../assets/images/favicon.ico" type="image/x-icon">
        <script>
            // 统计代码
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-93231524-1', 'auto');
            ga('send', 'pageview');
        </script>
    </head>
    <body>
        <div id="header">
            <a href="../index.html"><div id="logo">JG</div></a>
        </div>
        <div id="container" class="typo">
            <div id="article">
                <h1>大话西游答题器 command line</h1>
                <h4>Posted January 15, 2016</h4>

                <p>科举，大理寺都适用。 唯独不支持的是殿试。</p>

<h2>项目代吗</h2>

<p>需要依赖<a href="http://docs.python-requests.org/en/latest/">requests</a></p>
<div class="code-wrapper"><span class="lang-label">Bash</span><div class="highlight"><pre><span></span>pip install requests
</pre></div>
</div>
<p>然后保存下列代码到一个python文件(如: <code>search.py</code>)</p>
<div class="code-wrapper"><span class="lang-label">Python</span><div class="highlight"><pre><span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">File Name: search.py</span>
<span class="sd">Author: JackeyGao</span>
<span class="sd">mail: junqi.gao@shuyun.com</span>
<span class="sd">Created Time: 三  1/13 11:12:32 2016</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">readline</span>
<span class="kn">import</span> <span class="nn">signal</span>
<span class="kn">import</span> <span class="nn">requests</span>

<span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s1">&#39;tab: complete&#39;</span><span class="p">)</span>
<span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s1">&#39;set editing-mode vi&#39;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">_wrap_with_code</span><span class="p">(</span><span class="n">code</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">bold</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">code</span>
        <span class="k">if</span> <span class="n">bold</span><span class="p">:</span>
            <span class="n">c</span> <span class="o">=</span> <span class="s2">&quot;1;</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">c</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\033</span><span class="s2">[</span><span class="si">%s</span><span class="s2">m</span><span class="si">%s</span><span class="se">\033</span><span class="s2">[0m&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">inner</span>

<span class="n">red</span> <span class="o">=</span> <span class="n">_wrap_with_code</span><span class="p">(</span><span class="s1">&#39;31&#39;</span><span class="p">)</span>
<span class="n">green</span> <span class="o">=</span> <span class="n">_wrap_with_code</span><span class="p">(</span><span class="s1">&#39;32&#39;</span><span class="p">)</span>
<span class="n">blue</span> <span class="o">=</span> <span class="n">_wrap_with_code</span><span class="p">(</span><span class="s1">&#39;34&#39;</span><span class="p">)</span>


<span class="n">categorys</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;&#39;</span><span class="p">,</span>
        <span class="s1">&#39;乡试&#39;</span><span class="p">,</span>
        <span class="s1">&#39;省试-地理&#39;</span><span class="p">,</span>
        <span class="s1">&#39;省试-文学&#39;</span><span class="p">,</span>
        <span class="s1">&#39;省试-常识&#39;</span><span class="p">,</span>
        <span class="s1">&#39;省试-饮食&#39;</span><span class="p">,</span>
        <span class="s1">&#39;省试-历史&#39;</span><span class="p">,</span>
        <span class="s1">&#39;大话常识&#39;</span>
        <span class="p">)</span>

<span class="k">try</span><span class="p">:</span>
    <span class="n">category</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">category</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">categorys</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">IndexError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
    <span class="n">category</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">red</span><span class="p">(</span><span class="s2">&quot;CategoryError:</span><span class="se">\n</span><span class="s2">无效的科目&#39;</span><span class="si">%s</span><span class="s2">&#39;,支持:(</span><span class="si">%s</span><span class="s2">, &#39;&#39;(所有))</span><span class="se">\n</span><span class="s2">&quot;</span> \
            <span class="o">%</span> <span class="p">(</span><span class="n">category</span><span class="p">,</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">categorys</span><span class="p">))))</span>
    <span class="nb">exit</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="n">collect</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
    <span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://xy2-tiku.webapp.163.com/tiku/search&quot;</span>
    <span class="n">params</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">&quot;q&quot;</span><span class="p">:</span> <span class="n">query</span> <span class="p">}</span>
    <span class="k">if</span> <span class="n">collect</span><span class="p">:</span>
        <span class="n">params</span><span class="p">[</span><span class="s2">&quot;c&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">collect</span>

    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;Content-Type&quot;</span><span class="p">:</span> <span class="s2">&quot;application/json&quot;</span><span class="p">,</span>
            <span class="s2">&quot;Accept-Language&quot;</span><span class="p">:</span> <span class="s2">&quot;zh-CN,zh;q=0.8,en;q=0.6&quot;</span><span class="p">,</span>
            <span class="s2">&quot;Connection&quot;</span><span class="p">:</span> <span class="s2">&quot;keep-alive&quot;</span><span class="p">,</span>
            <span class="s2">&quot;Referer&quot;</span><span class="p">:</span> <span class="s2">&quot;http://dhxy.163.com/&quot;</span>
            <span class="p">}</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
            <span class="n">url</span><span class="p">,</span>
            <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span>
            <span class="n">headers</span><span class="o">=</span><span class="n">headers</span>
            <span class="p">)</span>
    <span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;data&quot;</span><span class="p">]:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;- [</span><span class="si">%s</span><span class="s2">] &quot;</span> <span class="o">%</span> <span class="n">q</span><span class="p">[</span><span class="s2">&quot;category&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">blue</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="s2">&quot;question&quot;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;&gt; &quot;</span> <span class="o">+</span> <span class="n">green</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="s2">&quot;answers&quot;</span><span class="p">]))</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">signal_handler</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">frame</span><span class="p">):</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">red</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">You pressed Ctrl+C! 答题终止.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>


<span class="n">signal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGINT</span><span class="p">,</span> <span class="n">signal_handler</span><span class="p">)</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
    <span class="n">collect</span> <span class="o">=</span> <span class="n">category</span> <span class="ow">or</span> <span class="s1">&#39;所有&#39;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">keyword</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s2">&quot;[</span><span class="si">%s</span><span class="s2">]输入关键字(终止Ctrl+C): &quot;</span> <span class="o">%</span> <span class="n">red</span><span class="p">(</span><span class="n">collect</span><span class="p">))</span>
        <span class="n">show</span><span class="p">(</span><span class="n">request</span><span class="p">(</span><span class="n">category</span><span class="p">,</span> <span class="n">keyword</span><span class="p">))</span>
    <span class="k">except</span> <span class="ne">EOFError</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">red</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">快捷键错误, 使用Ctrl+C 退出.&quot;</span><span class="p">))</span>
</pre></div>
</div>
<h2>使用方式</h2>
<div class="code-wrapper"><span class="lang-label">Bash</span><div class="highlight"><pre><span></span>$ python search.py <span class="nv">$CATEGORY</span>

<span class="c1"># CATEGORY 可以为空为空则为所有科目</span>
<span class="c1"># CATEGORY 支持(乡试,省试-地理,省试-文学,省试-常识,省试-饮食,省试-历史,大话常识, &#39;&#39;(所有))</span>
</pre></div>
</div>
<p><p class="hassubimage"><img src="/uploads/images/QQ20160115-1.png" alt="使用截图"></p>
</p>

            </div>
            <div id="footer">
                <a href="../words.html"><div id="more-words">MORE WORDS</div></a>
            </div>
        </div>
    </body>
</html>